﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; using Montana.Entidades;
using Montana.Reportes;
using CrystalDecisions.Shared;

namespace Montana
{
    public partial class frmImprimirRemito : Form
    {
        public frmImprimirRemito()
        {
            InitializeComponent();
        }

        public int IDFactura { get; set; }

        private void btnImprimir_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            GuardarConfiguracion();

            MostrarReporte();

            this.Cursor = Cursors.Default;
            
            this.Close();
        }

        private void GuardarConfiguracion()
        {
            Factura fac = new Factura();

            //si se ocultaron los cheques o no hay cheques para mostrar, los oculta
            string cheques = "N";
            if (chkMostrarCheques.Checked && frmFactura.Instancia.lswCheque.Items.Count != 0)
                cheques = "S";

            //si se ocultaron los resagos o no hay cheques para mostrar, los oculta
            string resagos = "N";
            if (chkMostrarResagos.Checked && frmFactura.Instancia.lswResago.Items.Count != 0)
                resagos = "S";

            string efectivo = chkMostrarEfectivo.Checked ? "S" : "N";
            string saldos = chkMostrarSaldos.Checked ? "S" : "N";
            string datosContables = chkMostrarDatosContables.Checked ? "S" : "N";
            string saldoResagos = chkSaldoResagos.Checked ? "S" : "N";

            fac.GuardarConfiguracionReporte(IDFactura, efectivo, cheques, resagos, saldos, datosContables, saldoResagos);
        }

        private void CargarConfiguracion()
        {
            Factura fac = new Factura();

            fac = fac.BuscarPorID(IDFactura);

            chkMostrarEfectivo.Checked = fac.Imprimirefectivo == "S" ? true : false;
            chkMostrarCheques.Checked = fac.Imprimircheques == "S" ? true : false;
            chkMostrarResagos.Checked = fac.Imprimirresagos == "S" ? true : false;
            chkMostrarSaldos.Checked = fac.Imprimirsaldos == "S" ? true : false;
        }

        private void MostrarReporte()
        {
            Reporte r = new Reporte();
            rptRemito remito = new rptRemito();
            DataSet ds = new DataSet();
            Factura fac = new Factura();
            Cheque che = new Cheque();
            Resago res = new Resago();

            fac.ReporteRemito(ds, IDFactura);
            che.ReporteRemito(ds, IDFactura);
            res.ReporteRemito(ds, IDFactura);

            string selectionFormula = "{Factura.ID} = " + IDFactura;

            ParameterFields parametros = new ParameterFields();

            r.Formulas.Add("OcultarEfectivo", "S");

            r.MostrarReporte(ds, remito, selectionFormula, parametros);
        }

        private void frmImprimirRemito_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    btnCancelar_Click(this, null);
                    break;
                case Keys.F1:
                    chkMostrarEfectivo.Checked = !chkMostrarEfectivo.Checked;
                    break;
                case Keys.F2:
                    chkMostrarCheques.Checked = !chkMostrarCheques.Checked;
                    break;
                case Keys.F3:
                    chkMostrarResagos.Checked = !chkMostrarResagos.Checked;
                    break;
                case Keys.F4:
                    chkMostrarSaldos.Checked = !chkMostrarSaldos.Checked;
                    break;
                case Keys.F5:
                    chkMostrarDatosContables.Checked = !chkMostrarDatosContables.Checked;
                    break;
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmImprimirRemito_Load(object sender, EventArgs e)
        {
            CargarConfiguracion();
        }
    }
}
